<template>
  <div class="adminList-wrap">
    <el-input
      v-model="searchInput"
      style="width: 340px; margin-bottom: 30px"
      size="large"
      placeholder="请输入搜索内容"
      :prefix-icon="Search"
      clearable
      @input="fnInput"
    />
    <el-table
      :data="datas"
      border
      style="width: 100%"
      :header-cell-style="{ background: '#f5f5f5', color: '#606266' }"
    >
      <el-table-column label="名称" min-width="200" align="center">
        <template #default="scope">{{
          scope.row.api_name ? scope.row.api_name : '-'
        }}</template>
      </el-table-column>
      <el-table-column label="描述" min-width="200" align="center">
        <template #default="scope">{{
          scope.row.api_desc ? scope.row.api_desc : '-'
        }}</template>
      </el-table-column>
      <el-table-column label="分类" min-width="140" align="center">
        <template #default="scope">{{
          scope.row.sort_name ? scope.row.sort_name : '-'
        }}</template>
      </el-table-column>
      <el-table-column label="用户" min-width="160" align="center">
        <template #default="scope">
          <span v-if="scope.row.need_token === 1">需要登录</span>
          <span class="no_txt" v-else-if="scope.row.need_token === 0"
            >无需登录</span
          >
        </template>
      </el-table-column>
      <el-table-column label="时间戳" min-width="100" align="center">
        <template #default="scope">
          <span v-if="scope.row.ts_filter === 1">校验</span>
          <span class="no_txt" v-else-if="scope.row.ts_filter === 0"
            >不校验</span
          >
        </template>
      </el-table-column>
      <el-table-column label="并发" min-width="80" align="center">
        <template #default="scope">{{
          scope.row.max_cc ? scope.row.max_cc : '-'
        }}</template>
      </el-table-column>
      <el-table-column label="状态" min-width="100" align="center">
        <template #default="scope">
          <span v-if="scope.row.status == 0">
            <el-button
              class="upload"
              size="small"
              type="success"
              @click="frozenFn(scope.$index, scope.row)"
              >冻结</el-button
            >
          </span>
          <span v-if="scope.row.status == -1">
            <el-button
              class="upload"
              size="small"
              type="warning"
              @click="activeFn(scope.$index, scope.row)"
              >激活</el-button
            >
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        min-width="100"
        fixed="right"
      >
        <template #default="scope">
          <el-button type="warning" size="small" @click="fnModify(scope.row)">
            修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <el-pagination
      class="page-warp"
      background
      layout="total,prev, pager, next, sizes"
      v-model:page-size="pageSize"
      :total="total"
      :page-sizes="[10, 20, 30, 40]"
      :current-page="currentPage"
      @current-change="fnCurrentChange"
      @size-change="fnSizeChange"
    />
  </div>
</template>

<script setup>
import { reactive, ref, onMounted, watch } from 'vue'
import { useRouter } from 'vue-router'
import { postData } from '@/http/request.js'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Plus, Search } from '@element-plus/icons-vue'
let router = useRouter()

let datas = ref([])
let res_datas = ref([])

let total = ref(0)
let currentPage = ref(1)
let pageSize = ref(10)
let searchInput = ref('')

//init
let getList = () => {
  postData(
    '/api/list',
    {
      with_details: 1,
    },
    {
      timeout: 120000,
    }
  )
    .then((res) => {
      if (res.code == 0) {
        total.value = res?.datas?.apis?.length || 0
        res_datas.value = res?.datas?.apis || []
        datas.value = res_datas.value.slice(0, pageSize.value)
      } else if (res.code == -56055) {
        return
      } else {
        ElMessage({
          type: 'error',
          message: 'error',
        })
      }
    })
    .catch((res) => {
      ElMessage({
        type: 'error',
        message: 'error',
      })
    })
}

//删除,当前行
let fnDelete = (dataRow) => {
  ElMessageBox.confirm('确认删除该用户?', '删除确认', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  })
    .then(() => {
      postData('/sys/admin/delete', {
        user_wanted: dataRow.user_name,
      }).then((res) => {
        if (res.code == 0) {
          ElMessage({
            type: 'success',
            message: '删除成功',
          })
          currentPage.value = 1
          getList()
        } else {
          ElMessage({
            type: 'error',
            message: '删除失败',
          })
        }
      })
    })
    .catch(() => {})
}

let fnRegister = () => {
  router.push('/adminRegister')
}

let fnPermission = (data) => {
  router.push('/adminPermission?id=' + data.user_name)
}

let fnModify = (data) => {
  router.push('/apiModify?api_name=' + data.api_name)
}

//分页
let fnSizeChange = (data) => {
  pageSize.value = data
  currentPage.value = 1
  getList()
}
let fnCurrentChange = (data) => {
  let start = (data - 1) * pageSize.value
  currentPage.value = data
  datas.value = res_datas.value.slice(start, start + pageSize.value)
}

//search
let fnInput = (val) => {
  if (val) {
    datas.value = datas.value.filter((item) => {
      return Object.keys(item).some((key) => {
        return String(item[key]).toLowerCase().indexOf(val) > -1
      })
    })
  } else {
     getList()
  }
  currentPage.value = 1
  total.value = datas.value.length
}

//点击冻结按钮
let frozenFn = (index, row) => {
  var api_name = row.api_name
  postData('/api/update/status', {
    api_name: api_name,
    status: -1,
  })
    .then((res) => {
      if (res.code == 0) {
        ElMessage.success('已激活')
        getList()
      } else {
        ElMessage.error(res.code + '，' + res.msg)
      }
    })
    .catch((res) => {
      ElMessage.error(res.code + '，' + res.msg)
    })
}

//点击激活按钮
let activeFn = (index, row) => {
  var api_name = row.api_name
  postData('/api/update/status', {
    api_name: api_name,
    status: 0,
  })
    .then((res) => {
      if (res.code == 0) {
        ElMessage.success('已冻结')
        getList()
      } else {
        ElMessage.error(res.code + '，' + res.msg + 3)
      }
    })
    .catch((res) => {
      ElMessage.error(res.code + '，' + res.msg + 4)
    })
}

onMounted(() => {
  getList()
})
</script>

<style scoped>
.adminList-wrap {
  width: 100%;
  height: 100%;
  position: relative;
}
.search-wrap {
  display: flex;
  justify-content: flex-start;
}

.adminList-wrap >>> .el-table__header-wrapper {
  background: red;
}
.adminList-wrap >>> .el-table__header {
  background: red;
}
.page-warp {
  float: right;
  margin-top: 10px;
}
</style>
